<?php

namespace app\common\model;

use think\Exception;
use think\facade\Log;

class Base extends TimeModel
{
    protected $deleteTime = "delete_time";

    /**
     * @param int $id
     * @param string $field
     * @param string|array $with
     * @return array
     * @throws Exception
     */
    public function getBaseById(int $id = 0, string $field = '*', $with = null): array
    {
        try {
            $result = $this->field($field)
                ->where('id', $id)
                ->with($with)
                ->find();
        } catch (\Exception $e) {
            Log::error($e->getMessage() . " " . $e->getFile() . " " . $e->getLine());
            throw new Exception("内部发生错误");
        }
        if ($result) {
            return $result->toArray();
        } else {
            return [];
        }
    }

    /**
     * @param array $where
     * @param string $field
     * @return array
     * @throws Exception
     */
    public function getBaseByWhereOne(array $where = [], string $field = '*'): array
    {
        try {
            $result = $this->field($field)
                ->where($where)
                ->find();
        } catch (\Exception $e) {
            Log::error($e->getMessage() . " " . $e->getFile() . " " . $e->getLine());
            throw new Exception("内部发生错误");
        }
        if ($result) {
            return $result->toArray();
        } else {
            return [];
        }
    }

    /**
     * 根据条件查询指定数据
     * @param array $where
     * @param string $field
     * @param string $order
     * @return array
     * @throws Exception
     */
    public function getBaseByWhere(array $where = [], string $field = '*', string $order = 'id desc'): array
    {
        try {
            $result = $this->field($field)
                ->where($where)
                ->order($order)
                ->select();
        } catch (\Exception $e) {
            Log::error($e->getMessage() . " " . $e->getFile() . " " . $e->getLine());
            throw new Exception("内部发生错误");
        }
        if ($result) {
            return $result->toArray();
        } else {
            return [];
        }
    }

    /**
     * 根据条件查询指定数据
     * @param array $where
     * @param string $field
     * @return int
     * @throws Exception
     */
    public function getBaseByWhereCount(array $where = [], string $field = '*'): int
    {
        try {
            $result = $this->field($field)
                ->where($where)
                ->count();
        } catch (\Exception $e) {
            Log::error($e->getMessage() . " " . $e->getFile() . " " . $e->getLine());
            throw new Exception("内部发生错误");
        }

        return $result;

    }

    /**
     * @param array $where
     * @param string $field
     * @param string $orderBy
     * @return array
     * @throws Exception
     */
    public function getBaseByList(array $where = [], string $field = '*', string $orderBy = "id desc"): array
    {
        try {
            $result = $this->field($field)
                ->where($where)
                ->order($orderBy)
                ->select();
        } catch (\Exception $e) {
            throw new Exception("查询失败");
        }
        if ($result) {
            return $result->toArray();
        } else {
            return [];
        }
    }

    /**
     * 分页方法
     * @param array $where
     * @param int $page
     * @param int $limit
     * @param string $field
     * @param string $orderBy
     * @return array
     * @throws Exception
     */
    public function getBaseBylistPage(array $where = [], int $page = 1, int $limit = 10, string $field = '*', string $orderBy = 'id desc'): array
    {
        try {
            $result = $this->field($field)
                ->where($where)
                ->order($orderBy)
                ->page($page, $limit)
                ->select()->toArray();
        } catch (\Exception $e) {
            throw new Exception("查询失败");
        }
        return $result;
    }

    /**
     * @param array $where
     * @param string $field
     * @return array
     * @throws Exception
     */
    public function getBaseByTrashedFindInfo(array $where = [], string $field = "*"): array
    {
        try {
            $result = $this->field($field)->where($where)->withTrashed()->find();
        } catch (\Exception $e) {
            throw new Exception("查询失败");
        }
        if ($result) {
            return $result->toArray();
        } else {
            return [];
        }
    }


}